<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '../../stores/auth'
import { User, Lock } from '@element-plus/icons-vue'

const router = useRouter()
const authStore = useAuthStore()

const form = ref({
  username: '',
  password: ''
})

const loading = ref(false)
const errorMessage = ref('')

const handleLogin = async () => {
  if (loading.value) return
  
  loading.value = true
  errorMessage.value = ''
  
  try {
    const success = authStore.login(form.value.username, form.value.password)
    if (success) {
      router.push('/')
    } else {
      errorMessage.value = '用户名或密码错误'
    }
  } catch (error) {
    errorMessage.value = '登录失败，请重试'
  } finally {
    loading.value = false
  }
}

const handleKeyPress = (e: KeyboardEvent) => {
  if (e.key === 'Enter') {
    handleLogin()
  }
}
</script>

<template>
  <div class="login-container">
    <div class="login-content">
      <div class="login-header">
        <div class="avatar-container">
          <img src="/touxiang.png" alt="Avatar" class="avatar" />
        </div>
        <h1>健康养老管理系统</h1>
        <p class="subtitle">让生活更美好</p>
      </div>
      
      <el-card class="login-card">
        <el-form @submit.prevent="handleLogin" @keyup="handleKeyPress">
          <el-form-item>
            <el-input
              v-model="form.username"
              placeholder="初九"
              size="large"
            >
              <template #prefix>
                <el-icon><User /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          
          <el-form-item>
            <el-input
              v-model="form.password"
              type="password"
              placeholder="密码"
              size="large"
              show-password
            >
              <template #prefix>
                <el-icon><Lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          
          <el-form-item v-if="errorMessage" class="error-message">
            {{ errorMessage }}
          </el-form-item>
          
          <el-button
            type="primary"
            :loading="loading"
            size="large"
            class="login-button"
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<style scoped lang="scss">
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, var(--primary-bg) 0%, var(--secondary-bg) 100%);
  
  .login-content {
    width: 100%;
    max-width: 400px;
    padding: 0 20px;
    
    .login-header {
      text-align: center;
      margin-bottom: 40px;
      
      .avatar-container {
        margin-bottom: 24px;
        
        .avatar {
          width: 100px;
          height: 100px;
          border-radius: 50%;
          object-fit: cover;
          border: 4px solid white;
          box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
          transition: transform 0.3s ease;
          
          &:hover {
            transform: scale(1.05);
          }
        }
      }
      
      h1 {
        font-size: 28px;
        font-weight: 600;
        color: var(--primary-text);
        margin: 0 0 8px;
      }
      
      .subtitle {
        font-size: 16px;
        color: var(--secondary-text);
        margin: 0;
      }
    }
    
    .login-card {
      backdrop-filter: blur(10px);
      background: rgba(255, 255, 255, 0.9);
      
      .el-form-item {
        margin-bottom: 24px;
      }
      
      .error-message {
        color: #ff4d4f;
        text-align: center;
        margin-bottom: 16px;
      }
      
      .login-button {
        width: 100%;
      }
    }
  }
}
</style>